package com.hz.recyclerviewcardgallery;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.hz.recyclerviewcardgallery.view.CardScaleHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private ImageView mBlurView;
    private List<Integer> mList = new ArrayList<>();
    private CardScaleHelper mCardScaleHelper = null;
    private LinearLayoutManager linearLayoutManager;
    private boolean isLoadingMore = true;
    private int mLastPos = -1;
    private CardAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        for (int i = 0; i < 3; i++) {
            mList.add(R.drawable.pic4);
            mList.add(R.drawable.pic5);
            mList.add(R.drawable.pic6);
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new CardAdapter();
        mRecyclerView.setAdapter(mAdapter);
        // mRecyclerView绑定scale效果
        mCardScaleHelper = new CardScaleHelper();
        mCardScaleHelper.setCurrentItemPos(0);  //默认选中第一个
        mCardScaleHelper.attachToRecyclerView(mRecyclerView);

        mAdapter.setData(mList);

        initBlurBackground();
    }

    private void initBlurBackground() {
        mBlurView = (ImageView) findViewById(R.id.blurView);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (mLastPos == mCardScaleHelper.getCurrentItemPos()) return;   //防止一直调用
                    mLastPos = mCardScaleHelper.getCurrentItemPos();



                    Log.d("TAG","===========加载当页数据=========="+mLastPos);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = (linearLayoutManager).findLastVisibleItemPosition();
                int totalItemCount = linearLayoutManager.getItemCount();
                //totalItemCount - 1 表示滑动到最后一个时
                // dx>0 表示向右边滑动
                if (lastVisibleItem == (totalItemCount - 1) && dx > 0) {
                    if(isLoadingMore){
                        Log.d("TAG","============加载更多==========");
                        isLoadingMore = false;
                        loadingMore();
                    }
                }
            }
        });
    }

    //加载更多数据
    private void loadingMore(){
        for (int i = 0; i < 1; i++) {
            mList.add(R.drawable.pic4);
            mList.add(R.drawable.pic5);
            mList.add(R.drawable.pic6);
        }
        mAdapter.setData(mList);
        isLoadingMore = true;

    }






    /*private void notifyBackgroundChange() {
        if (mLastPos == mCardScaleHelper.getCurrentItemPos()) return;
        mLastPos = mCardScaleHelper.getCurrentItemPos();
        final int resId = mList.get(mCardScaleHelper.getCurrentItemPos());
        mBlurView.removeCallbacks(mBlurRunnable);

        //背景虚化图片
//        mBlurRunnable = new Runnable() {
//            @Override
//            public void run() {
//                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
//                ViewSwitchUtils.startSwitchBackgroundAnim(mBlurView, BlurBitmapUtils.getBlurBitmap(mBlurView.getContext(), bitmap, 15));
//            }
//        };

        mBlurView.postDelayed(mBlurRunnable, 500);
    }*/

}
